/*
 * Copyright © Jack魏 2022 - 2022, All Rights Reserved.
 */

package com.jack.thread.t02;

/**
 * Join 方法使用
 *
 * @author Jack魏
 * @since 2022/11/19 22:45
 */
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException {
        JoinThread thread1 = new JoinThread();
        JoinThread thread2 = new JoinThread();
        JoinThread thread3 = new JoinThread();

        thread1.start();
        // 必须要在start之后join，否则会失效。
        // 再此后面的线程都要等待本线程执行完，才能执行。
        // 再此之前的线程是并发一起的执行的
        thread1.join();

        // 必须执行完thread1，后面的线程再执行
        thread2.start();
        // 如果加上这一句，三个线程就变成串行的了
        // thread2.join();
        thread3.start();
    }
}

/**
 * 线程类
 */
class JoinThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + " while running " + i);
        }
    }
}
